// 本文件存放各种全局通用函数或者方法
// 只有具有极高抽象性的才可以存放在此
// 个别与页面紧密相连的方法函数
// 可以存放在view.js中(这个文件用于分类管理页面通用函数)
const AppUtil = {};
// 对随机数的扩展
AppUtil.Random = {
  // 该方法返回一个[min,max]内的随机整数
  getNumber: function (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  },
  // 该方法返回一个随机布尔值
  getBool: function () {
    let val = this.getNumber(0, 1);
    if (val == 0) return false;
    return true;
  },
};
// 选择
AppUtil.Select = {
  // 下一首
  slem: function (audio, swiper, type) {
    // 初始化
    if (GroupVariable.NowMusicId < 0) {
      GroupVariable.NowMusicId++;
      if (type == GroupEnum.ModEnum.RANDOM_NETMUSIC) {
        AppUtil.Music.fetNetMusic(audio, swiper);
        ViewUtil.Rond.rondMList(GroupVariable.MusicList,type);
      } else if (type == GroupEnum.ModEnum.SLEEP_MUSIC) {
        AppUtil.Music.fetSleepMusic(audio, swiper);
        ViewUtil.Rond.rondMList(GroupVariable.MusicList,type);
      } else if(type == GroupEnum.ModEnum.SEARCH_LIST){
        // NOTHING...
      }else{
        // NOTHING...
      }
      return;
    }
    // 使用列表
    // 判断是否到达末尾并且持有缓存
    if (
      GroupVariable.NowMusicId == GroupVariable.MusicList.length - 1 &&
      GroupVariable.NextMusicData != null &&
      GroupVariable.IsInitNextM
    ) {
      let data = GroupVariable.NextMusicData;
      GroupVariable.MusicList.push(data);
      GroupVariable.NowMusicId++;
      GroupVariable.NextMusicData = null;
      GroupVariable.IsInitNextM = false;
      audio.src = data.url;
      audio.play();
      ViewUtil.MusicColl.updataSM(data.song, data.img,audio,data.singer);
      ViewUtil.MusicColl.roat(true);
      ViewUtil.Rond.rondSName(data.song,null,data.singer);
      ViewUtil.Rond.rondMList(GroupVariable.MusicList,type);
      AppUtil.Music.getNetMusicData().then((data) => {
        // 处理缓存
        GroupVariable.NextMusicData = data;
        GroupVariable.IsInitNextM = true;
        ViewUtil.Rond.rondSName(null,data.song);
      });
      return;
    }

    // 判断是否在列表中间
    if (
      (GroupVariable.MusicList.length - 1 != GroupVariable.NowMusicId) &&
      (GroupVariable.MusicList.length != 0)
    ) {
      GroupVariable.NowMusicId++;
      let data = GroupVariable.MusicList[GroupVariable.NowMusicId];
      audio.src = data.url;
      audio.play();
      ViewUtil.MusicColl.updataSM(data.song, data.img,audio,data.singer);
      ViewUtil.MusicColl.roat(true);
      ViewUtil.Rond.rondSName(data.song,GroupVariable.MusicList[GroupVariable.NowMusicId + 1].song,data.singer);
      return;
    }

    // 正常请求
    GroupVariable.NowMusicId++;
    if (type == GroupEnum.ModEnum.RANDOM_NETMUSIC) {
      AppUtil.Music.fetNetMusic(audio, swiper);
      ViewUtil.Rond.rondMList(GroupVariable.MusicList,type);
    } else if (type == GroupEnum.ModEnum.SLEEP_MUSIC) {
      AppUtil.Music.fetSleepMusic(audio, swiper);
      ViewUtil.Rond.rondMList(GroupVariable.MusicList,type);
    } else if (type == GroupEnum.ModEnum.SEARCH_LIST) {
      // 搜索到达请求
      if (GroupVariable.NowMusicId == GroupVariable.SearchDataTemp){
        GroupVariable.NowMusicId = 0;
        AppUtil.Music.updateNetGet(1,GroupVariable.tempSear,GroupVariable.AudOb,false);
        return;
      }
      AppUtil.Music.updateNetGet(GroupVariable.NowMusicId + 1,GroupVariable.tempSear,GroupVariable.AudOb,false);
      // 渲染数据
      let datas = new Array();
      GroupVariable.searchTemp.forEach((data, index) => {
        let temp = GroupVariable.saveParseData[index];
        if (temp != null) {
          datas.push(temp);
          return;
        }
        datas.push({
          title: data.title,
          singer: data.singer,
          cover: "images/icon/netmusic.png",
        });
      });
      ViewUtil.Rond.rondMList(datas, type);
      GroupVariable.PlayListSwiper.slideTo(GroupVariable.NowMusicId,1);
    }else{
      // QQ音乐到达末尾处理程序
      GroupVariable.NowMusicId = 0;
      let data = GroupVariable.MusicList[0];
      audio.src = data.url;
      audio.play();
      ViewUtil.MusicColl.updataSM(data.song, data.img,audio,data.singer);
      ViewUtil.MusicColl.roat(true);
      ViewUtil.Rond.rondSName(data.song,GroupVariable.MusicList[GroupVariable.NowMusicId + 1].song,data.singer);
    }
    return;
  },
  // 上一首
  priv: function (audio, swiper, type) {
    if (GroupVariable.NowMusicId < 0) return;
    if (GroupVariable.NowMusicId == 0 && (type != GroupEnum.ModEnum.SEARCH_LIST)) {
      GroupVariable.NowMusicId = GroupVariable.MusicList.length;
    }else if(type == GroupEnum.ModEnum.SEARCH_LIST){
      // 单独处理搜索
      if(GroupVariable.NowMusicId == 0){
        AppUtil.Music.updateNetGet(GroupVariable.SearchDataTemp,GroupVariable.tempSear,GroupVariable.AudOb);
        return;
      }
      AppUtil.Music.updateNetGet(GroupVariable.NowMusicId,GroupVariable.tempSear,GroupVariable.AudOb,false);
      GroupVariable.NowMusicId--;
      return;
    }else{
      // NOTHING...
    }
    GroupVariable.NowMusicId--;
    let data = GroupVariable.MusicList[GroupVariable.NowMusicId];
    ViewUtil.Rond.rondSName(data.song,GroupVariable.MusicList[GroupVariable.NowMusicId + 1].song,data.singer);
    audio.src = data.url;
    audio.play();
    ViewUtil.MusicColl.updataSM(data.song, data.img,audio,data.singer);
    ViewUtil.MusicColl.roat(true);
  }
};
// 对音乐播放的更新
AppUtil.Music = {
  // WIFI GET UPDATE
  updateNetGet(n,tempSear,audio,needAuto = true){
    let mus_url;
    let url;
    if(needAuto){
      GroupVariable.NowMusicId = (n - 1);
    }
    if(GroupVariable.saveParseData[n - 1] != null){
      let data = GroupVariable.saveParseData[n - 1];
      GroupVariable.IsInitNextM = false;
      if(GroupVariable.searchMod == GroupEnum.SearchMod.NetEasyMusic){
        mus_url = data.music_url;
      }else{
        mus_url = data.music;
      }
      audio.src = mus_url;
      audio.play();
      ViewUtil.MusicColl.updataSM(data.title, data.cover,audio,data.singer);
      ViewUtil.MusicColl.roat(true);
      return;
    }
    if(GroupVariable.searchMod == GroupEnum.SearchMod.NetEasyMusic){
      url = `https://api.cenguigui.cn/api/music/netease/WyY_Dg.php?type=json&msg=${tempSear}&num=30&br=2&n=${n}`;
    }else{
      url = `http://api.cenguigui.cn/api/qishui/?msg=${tempSear}&type=json&n=${n}`;
    }
    let lid = layui.layer.load(1, {
      shade: 0.6,
    });
    fetch(url)
      .then((response) => response.json())
      .then((data) => {
        if(data.code != 200) return;
        data = data.data;
        GroupVariable.saveParseData[n - 1] = data;
        GroupVariable.IsInitNextM = false;
        if(GroupVariable.searchMod == GroupEnum.SearchMod.NetEasyMusic){
          mus_url = data.music_url;
        }else{
          mus_url = data.music;
        }
        audio.src = mus_url;
        audio.play();
        ViewUtil.MusicColl.updataSM(data.title, data.cover,audio,data.singer);
        ViewUtil.MusicColl.roat(true);
        layui.layer.close(lid);
      })
      .catch((e) => {
        console.log(e);
        mui.toast(e.toString());
        layui.layer.close(lid);
      });
  },
  // 仅获取网易云歌曲数据
  getNetMusicData() {
    return fetch("https://www.hhlqilongzhu.cn/api/wangyi_hot_review.php", {
      method: "GET",
      cache: "no-store",
      headers: {
        "Content-Type": "application/json",
      },
    })
      .then((response) => response.json())
      .then((data) => {
        return data;
      })
      .catch((e) => {
        mui.toast(e.toString());
        layui.layer.close(lid);
      });
  },
  // 仅获得助眠歌曲数据
  getSleepData() {
    return fetch("https://api.cenguigui.cn/api/music/ximalaya_gangqin.php", {
      method: "GET",
      cache: "no-store",
      headers: {
        "Content-Type": "application/json",
      },
    })
      .then((response) => response.json())
      .then((data) => {
        return data;
      })
      .catch((e) => {
        mui.toast(e.toString());
        layui.layer.close(lid);
      });
  },
  // 随机网易云歌曲
  fetNetMusic: function (audio, swiper) {
    let lid = layui.layer.load(1, {
      shade: 0.6,
    });
    this.getNetMusicData().then((data) => {
      GroupVariable.MusicList.push(data);
      audio.src = data.url;
      audio.play();
      ViewUtil.MusicColl.updataSM(data.song, data.img,audio,data.singer);
      ViewUtil.MusicColl.roat(true);
      layui.layer.close(lid);
      ViewUtil.Rond.rondSName(data.song,null,data.singer);
      return data;
    });
    this.getNetMusicData().then((data) => {
      GroupVariable.NextMusicData = data;
      GroupVariable.IsInitNextM = true;
      ViewUtil.Rond.rondSName(null,data.song);
    });
  },
  // 助眠钢琴曲
  fetSleepMusic(audio, swiper) {
    let lid = layui.layer.load(1, {
      shade: 0.6,
    });
    this.getSleepData().then((data) => {
      GroupVariable.MusicList.push(data);
      audio.src = data.url;
      audio.play();
      ViewUtil.MusicColl.updataSM(data.title, data.cover,audio,data.singer);
      ViewUtil.MusicColl.roat(true);
      layui.layer.close(lid);
      ViewUtil.Rond.rondSName(data.song,null,data.singer);
      return data;
    });
    this.getSleepData().then((data) => {
      GroupVariable.IsInitNextM = true;
      GroupVariable.NextMusicData = data;
      ViewUtil.Rond.rondSName(null,data.song);
    });
  },
  // QQ音乐
  fetQQMusic(isdt = true) {
    let lid;
    if(isdt){
      lid = layui.layer.load(1, {
        shade: 0.6,
      });
    }
    fetch("https://api.cenguigui.cn/api/qq/music/Daily30.php", {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
      },
    })
      .then((response) => response.json())
      .then((data) => {
        let dd = data.data.songlist;
        let arr = new Array();
        dd.forEach((val, ind) => {
          $(".qqm-coll").append(`
            <div class="qq-music-coll" data-qurl='${val.url}' data-n='${ind}'>
              <img src="${val.pic}">
              <span>${val.name}</span>
              <p>${val.author}</p>
            </div>
          `);
          arr.push({
            img: val.pic,
            url: val.url,
            song: val.name,
            singer: val.author,
          });
        });
        GroupVariable.QQMusicData = arr;
        layui.layer.close(lid);
      })
      .catch((e) => {
        mui.toast(e.toString());
        layui.layer.close(lid);
      });
  },
};
// 对搜索歌曲的检索
AppUtil.SraechOf = {
  // 网易云
  NetMusic(songtext) {
    let lid = layui.layer.load(1, {
      shade: 0.6,
    });
    fetch(
      `https://api.cenguigui.cn/api/music/netease/WyY_Dg.php?type=json&msg=${songtext}&num=30&br=2`,
      {
        method: "GET",
        cache: "no-store",
        headers: {
          "Content-Type": "application/json",
        },
      }
    )
      .then((response) => response.json())
      .then((data) => {
        let cdata = data.data;
        GroupVariable.searchTemp = cdata;
        ViewUtil.Rond.rondArray(cdata);
        GroupVariable.SearchDataTemp = cdata.length;
        GroupVariable.saveParseData = new Array(cdata.length);
        GroupVariable.saveParseData.fill(null);
        layui.layer.close(lid);
      })
      .catch((e) => {
        console.log(e);
        mui.toast(e.toString());
        layui.layer.close(lid);
      });
  },
  // 汽水
  QSMusic(tempSear){
    let lid = layui.layer.load(1, {
      shade: 0.6,
    });
    fetch(`https://api.cenguigui.cn/api/qishui/?msg=${tempSear}&type=json`)
      .then((response) => response.json())
      .then((data) => {
        let cdata = data.data;
        GroupVariable.searchTemp = cdata;
        ViewUtil.Rond.rondArray(cdata);
        GroupVariable.SearchDataTemp = cdata.length;
        GroupVariable.saveParseData = new Array(cdata.length);
        GroupVariable.saveParseData.fill(null);
        layui.layer.close(lid);
      })
      .catch((e) => {
        console.log(e);
        mui.toast(e.toString());
        layui.layer.close(lid);
      });
  }
};
